package com.neusoft.neumooc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.neusoft.core.common.BaseController;
import com.neusoft.core.common.conf.Params;
import com.neusoft.core.util.Util;
import com.neusoft.neumooc.VO.GoodsVO;
import com.neusoft.neumooc.VO.MenuVO;
import com.neusoft.neumooc.service.GoodsService;
import com.neusoft.neumooc.service.MenuService;


/** 
 * @ClassName: GoodsController 
 * @Description: TODO 商品管理控制类
 * @author zls
 * @date 2016年10月27日
 */
@Controller
@RequestMapping(value = "/goods")
public class GoodsController extends BaseController{
	
	/** 
	 * @Fields goodsService : TODO 商品管理业务逻辑处理类
	 */ 
	@Autowired
	GoodsService goodsService;
	
	/** 
	 * @Fields menuService : TODO 菜单管理业务逻辑处理类
	 */ 
	@Autowired
	MenuService menuService;
	
	
	/** 
	 * @Title: list 
	 * @Description: TODO 显示商品列表
	 * @param request
	 * @param response
	 * @param pageNo 起始页数
	 * @param pageSize 页面大小
	 * @return Map
	 * @throws Exception
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String,Object> list(HttpServletRequest request, HttpServletResponse response,String pageNo,String pageSize) throws Exception {
		Map<String, Object> retMap = new HashMap<String, Object>();
		int pageIndex = (Util.parseInt(pageNo) - 1) * Util.parseInt(pageSize);
		try {
			List<GoodsVO> goodsList = goodsService.getGoodsList(pageIndex,Util.parseInt(pageSize));
			int total = goodsService.getGoodsCount();
			retMap.put(Params.RET_CODE,Params.RET_CODE_SUCCESS);
			retMap.put("goodsList", goodsList);
			retMap.put("total", total);	
		} catch (Exception e) {
			log.error("显示商品列表有异常信息抛出" + e.getMessage(),e.getCause());
			retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
		}
		return retMap;
	}
	
		
	/** 
	 * @Title: add 
	 * @Description: TODO 添加商品
	 * @param request
	 * @param response
	 * @param goodsVO 商品实体类
	 * @return Map
	 * @throws Exception
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Map<String,Object> add(HttpServletRequest request, HttpServletResponse response, GoodsVO goodsVO) throws Exception {
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			int count = goodsService.addGoods(goodsVO);
			if(count > 0){
				retMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
				retMap.put(Params.RET_MSG, "添加商品成功");
			}else{
				retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
				retMap.put(Params.RET_MSG, "添加商品失败");
			}
		} catch (Exception e) {
			log.error("添加商品有异常信息抛出:" + e.getMessage(),e.getCause());
			retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
			retMap.put(Params.RET_MSG, "添加商品失败");
		}
		return retMap;
	}
	
	
	/** 
	 * @Title: edit 
	 * @Description: TODO 更改商品
	 * @param request
	 * @param response
	 * @param goodsVO 商品实体类
	 * @return Map
	 * @throws Exception
	 */
	@RequestMapping(value = "/edit")
	@ResponseBody
	public Map<String,Object> edit(HttpServletRequest request, HttpServletResponse response, GoodsVO goodsVO) throws Exception {
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			int count = goodsService.updateGoods(goodsVO);
			if(count > 0){
				retMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
				retMap.put(Params.RET_MSG, "更改商品成功");
			}else{
				retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
				retMap.put(Params.RET_MSG, "更改商品失败");
			}
			retMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
			retMap.put(Params.RET_MSG, "更改商品成功");
		} catch (Exception e) {
			log.error("更改商品有异常信息抛出:" + e.getMessage(),e.getCause());
			retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
			retMap.put(Params.RET_MSG, "更改商品失败");
		}
		return retMap;
	}
	
	
	/** 
	 * @Title: delete 
	 * @Description: TODO 删除商品
	 * @param request
	 * @param response
	 * @param id 商品ID
	 * @return Map
	 * @throws Exception
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String,Object> delete(HttpServletRequest request, HttpServletResponse response,String id) throws Exception {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			int count = goodsService.deleteGoods(id);
			if(count > 0){
				resMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
				resMap.put(Params.RET_MSG, "删除商品成功");
			}else{
				resMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
				resMap.put(Params.RET_MSG, "删除商品失败");
			}
		} catch (Exception e) {
			log.error("删除商品有异常信息抛出:" + e.getMessage(),e.getCause());
			resMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
			resMap.put(Params.RET_MSG, "删除商品失败");
		}
		return resMap;
	}
	
	
	/** 
	 * @Title: menu 
	 * @Description: TODO 获得菜单列表
	 * @param request
	 * @return Map
	 * @throws Exception
	 */
	@RequestMapping(value = "/menu")
	@ResponseBody
	public Map<String, Object> menu(HttpServletRequest request) throws Exception{
		Map<String, Object> resMap = new HashMap<String, Object>();
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			List<MenuVO> MenuList = menuService.getMenuList();
			retMap.put("MenuList", MenuList);
			resMap.put(Params.RET_OBJ, retMap);
			resMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("获得菜单列表有异常信息抛出:" + e.getMessage(),e.getCause());
			resMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
		}
		return resMap;
	}
	
	
	/** 
	 * @Title: info 
	 * @Description: TODO 获得商品详细内容
	 * @param request
	 * @param id 商品ID
	 * @return Map
	 * @throws Exception
	 */
	@RequestMapping(value = "/info")
	@ResponseBody
	public Map<String, Object> info(HttpServletRequest request,String id) throws Exception{
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			List<GoodsVO> showGoodsMsg = goodsService.getGoodsMsg(id);
			if(showGoodsMsg.isEmpty()){
				resMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);	
			}else{
				resMap.put("showGoodsMsg", showGoodsMsg);
				resMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);	
			}
		} catch (Exception e) {
			log.error("获得商品详细内容有异常信息抛出:" + e.getMessage(),e.getCause());
			resMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
		}
		return resMap;
	}
	
	
	/** 
	 * @Title: forMenu 
	 * @Description: TODO 获得分类下的商品
	 * @param request
	 * @param pageNo 起始页数
	 * @param pageSize 页面大小
	 * @param menuId 分类ID
	 * @return Map
	 * @throws Exception
	 */
	@RequestMapping(value = "/formenu")
	@ResponseBody
	public Map<String, Object> forMenu(HttpServletRequest request,String pageNo,String pageSize,String menuId) throws Exception{
		Map<String, Object> resMap = new HashMap<String, Object>();
		int pageIndex = (Util.parseInt(pageNo) - 1) * Util.parseInt(pageSize);
		try {
			List<GoodsVO> getListByMenu = goodsService.getListByMenu(pageIndex,Util.parseInt(pageSize),menuId);
			int total = goodsService.getMenuGoodsCount(menuId);
			resMap.put("total", total);
			resMap.put("getListByMenu", getListByMenu);
			resMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("获得商品详细内容有异常信息抛出:" + e.getMessage(),e.getCause());
			resMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
		}
		return resMap;
	}
}
